package com.chensi.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Test;

public class Sender {
	@Test
	public void test() throws JMSException {
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER, ActiveMQConnectionFactory.DEFAULT_PASSWORD,
				"tcp://127.0.0.1:61616");
		Connection connection = connectionFactory.createConnection();
		connection.start();
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		Destination destination=session.createQueue("queue1");
		MessageProducer messageProducer=session.createProducer(destination);
		messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);//非持久化
		for(int i=0;i<5;i++){
			TextMessage textMessage=session.createTextMessage();
			textMessage.setText("我是消息："+i);
			messageProducer.send(textMessage);
			System.out.println("我是消息："+i);
		}
		if(connection!=null){
			connection.close();
		}
	}
	
	@Test
	public void test2() throws JMSException {
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER, ActiveMQConnectionFactory.DEFAULT_PASSWORD,
				"tcp://127.0.0.1:61616");
		Connection connection = connectionFactory.createConnection();
		connection.start();
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		Destination destination=session.createQueue("queue1");
		MessageProducer messageProducer=session.createProducer(destination);
		messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);//非持久化
		
		TextMessage textMessage=session.createTextMessage();
		textMessage.setText("我是消息：0");
		messageProducer.send(destination,textMessage,DeliveryMode.NON_PERSISTENT,0,120*1000);
		
		TextMessage textMessage1=session.createTextMessage();
		textMessage1.setText("我是消息：9");
		messageProducer.send(destination,textMessage1,DeliveryMode.NON_PERSISTENT,9,120*1000);
		
		TextMessage textMessage2=session.createTextMessage();
		textMessage2.setText("我是消息：3");
		messageProducer.send(destination,textMessage2,DeliveryMode.NON_PERSISTENT,3,120*1000);
		
		session.commit();
		if(connection!=null){
			connection.close();
		}
	}
}
